package com.yanggu.jt808.model.resp;

import com.yanggu.jt808.consts.Jt808Constant;
import com.yanggu.jt808.model.base.DataPackage;
import com.yanggu.jt808.util.ByteArrHelper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;


@Data
public class Data0x8004 extends DataPackage {

    /**
     * BCD编码,6字节
     */
    private String timeHex;


    public Data0x8004() {
        this.getHeader().setMsgId(Jt808Constant.查询服务器时间应答);
    }

    @Override
    public ByteBuf toByteBufMsg() {

        ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
        //写入消息体
        buf.writeBytes(ByteArrHelper.hexStr2bytes(timeHex));
        super.setMsgBody(buf);
        return super.toByteBufMsg();
    }

    public static Data0x8004 success(DataPackage msg, int flowId) {
        Data0x8004 resp = new Data0x8004();

        //设置一些参数
        resp.copyBy(msg,flowId);

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        String timeHex = sdf.format(new Date(System.currentTimeMillis()));
        resp.setTimeHex(timeHex);

        return resp;
    }
}